package jcu.ict.profaid;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class AnswerDbAdapter {
	public static final String KEY_ID = "answer_id";
	public static final String KEY_USER = "user_id";
	public static final String KEY_QUESTION_ID = "question_id";
	public static final String KEY_COURSE_ID = "course_id";
	public static final String KEY_ANSWER = "answer_desc";
	public static final String KEY_ANSWER_DATE = "answer_date_time";
	public static String KEY_ANSWER_SUM = "answer_sum";
	public static final String KEY_TIME_FLAG = "time_flag";
	private static final String DATABASE_TABLE = "answer";

	private Context context;

	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;

	public AnswerDbAdapter(Context ctx) {
		this.context = ctx;
	}

	// --- open the database ---
	public AnswerDbAdapter open() throws SQLException {
		DBHelper = new DatabaseHelper(context);
		db = DBHelper.getWritableDatabase();

		return this;
	}

	// ---closes the database ---
	public void close() {
		DBHelper.close();
	}

	// --- insert Question to question table ---
	public long insertAnswerList(String user_id, String answer, String date, int question_id, String course_id, long flag_time) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_USER, user_id);
		initialValues.put(KEY_ANSWER, answer);
		initialValues.put(KEY_ANSWER_DATE, date);
		initialValues.put(KEY_QUESTION_ID, question_id);
		initialValues.put(KEY_COURSE_ID, course_id);
		initialValues.put(KEY_TIME_FLAG, flag_time);
		return db.insert(DATABASE_TABLE, null, initialValues);
	}

	// --- populate Question from database ---
	public Cursor getAnswer(int answer_id_audiance) throws SQLException {

		Cursor mCursor = db.query(true, DATABASE_TABLE,
				new String[] { KEY_ID }, KEY_ID + "=\"" + answer_id_audiance
						+ "\"", null, null, null, null, null);
		return mCursor;

	}
	
	public Cursor PopulateAnswer(String user_id, String Id) throws SQLException {

		Cursor mCursor =
				db.query(true, DATABASE_TABLE, new String[] {KEY_ANSWER,KEY_ANSWER_SUM,KEY_ID}, KEY_QUESTION_ID + "=\"" + Id +  "\"" + "AND " + KEY_USER + "=\"" + user_id +  "\"", null,
						null, null, null, null);
		return mCursor;
	}
	
	public Cursor PopulateEditAnswer(String Id) throws SQLException {

		Cursor mCursor =
				db.query(true, DATABASE_TABLE, new String[] {KEY_ANSWER,KEY_ID}, KEY_QUESTION_ID + "=\"" + Id +  "\"", null,
						null, null, null, null);
		return mCursor;
	}
	
	
	public Cursor TableAnswer(String Id) throws SQLException {

		Cursor mCursor =
				db.query(true, DATABASE_TABLE, new String[] {KEY_ID, KEY_ANSWER ,KEY_ANSWER_SUM}, KEY_QUESTION_ID + "=\"" + Id +  "\"", null,
						null, null, null, null);
		return mCursor;
	}
	
	
	public Cursor getTimeCompare() throws SQLException {

		Cursor mCursor = db.query(true, DATABASE_TABLE,
				new String[] { KEY_TIME_FLAG }, KEY_ID + "=\"" + "1"
						+ "\"", null, null, null, null, null);
		return mCursor;

	}

	public Cursor getSum(int answer_id_audiance) throws SQLException {

		Cursor mCursor = db.query(true, DATABASE_TABLE,
				new String[] { KEY_ANSWER_SUM }, KEY_ID + "=\""
						+ answer_id_audiance + "\"", null, null, null, null,
				null);
		return mCursor;

	}
	
	public boolean updateAnswer( String answerId, String answerEdited) {
		ContentValues args = new ContentValues();
		args.put(KEY_ANSWER, answerEdited);
		return db.update(DATABASE_TABLE, args,
				KEY_ID + "=\"" + answerId + "\"" , null) > 0;
	}
	
	public boolean ResetAnswer(String answerId) {
		ContentValues args = new ContentValues();
		args.put(KEY_ANSWER_SUM, 0);
		return db.update(DATABASE_TABLE, args,
				KEY_ID + "=\"" + answerId + "\"" , null) > 0;
	}
	
	public boolean updateAnswerSum(int AnswerId, int AnswerSum) {
		ContentValues args = new ContentValues();
		args.put(KEY_ANSWER_SUM, AnswerSum = AnswerSum + 1);
		return db.update(DATABASE_TABLE, args,
				KEY_ID + "=\"" + AnswerId + "\"", null) > 0;
	}
	
	public boolean updateTimeFlag(long time) {
		ContentValues args = new ContentValues();
		args.put(KEY_TIME_FLAG, time);
		return db.update(DATABASE_TABLE, args,
				null, null) > 0;
	}
}
